package com.lc.pianostudent.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.adapter.PianoIndexAdapter;
import com.lc.pianostudent.adapter.PianoTimeAdapter;
import com.lc.pianostudent.bean.PianoIndexBean;
import com.lc.pianostudent.bean.PianoTimeBean;
import com.lc.pianostudent.conn.PostRanking;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/25.
 */

public class FriendListActivity extends BaseActivity implements View.OnClickListener{

    @BoundView(value = R.id.junior,isClick = true) private TextView junior;
    @BoundView(value = R.id.middle,isClick = true) private TextView middle;
    @BoundView(value = R.id.high,isClick = true) private TextView high;
    @BoundView(value = R.id.last_week_question,isClick = true) private ImageView last_week_question;
    private int width = 720;
    private int height = 1280;
    private int degree = 0;
//
//
//    public List<PianoTimeBean> timeList = new ArrayList<>();
    @BoundView(R.id.friend_list_recycleview) private XRecyclerView recyclerView;
//    private PianoTimeAdapter pianoTimeAdapter;

    public List<PianoIndexBean> indexList = new ArrayList<>();
    private PianoIndexAdapter pianoIndexAdapter;
    private  PostRanking.PostRankingInfo currentInfo;
    private PostRanking postRanking = new PostRanking(new AsyCallBack<PostRanking.PostRankingInfo>() {


        @Override
        public void onSuccess(String toast, int type, Object o, PostRanking.PostRankingInfo postRankingInfo) throws Exception {

            if(type == 0){
                indexList.clear();
            }
            currentInfo = postRankingInfo;

            indexList.addAll(postRankingInfo.list);
            pianoIndexAdapter.notifyDataSetChanged();
//            Log.e("===curr",currentInfo.current_page+"===curr");
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {
            recyclerView.refreshComplete();
            recyclerView.loadMoreComplete();
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }

    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend_list);

        setBackTrue();

        initView();
    }

    private void initView(){
        setTitleName("答题排行榜");

        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels;         // 屏幕宽度（像素）
        height = dm.heightPixels;       // 屏幕高度（像素）
//        Log.e("=====jin",width + "--width");
//        Log.e("=====jin",height + "--height");

        junior.setBackgroundResource(R.mipmap.ph_button_select);
        middle.setBackgroundResource(R.mipmap.ph_button_default);
        high.setBackgroundResource(R.mipmap.ph_button_default);

        junior.getLayoutParams().height =  width/9;
        junior.getLayoutParams().width = (int) (width/4.24);

        middle.getLayoutParams().height = (int) (width/10.75);
        middle.getLayoutParams().width = (int) (width/4.24);

        high.getLayoutParams().height = (int) (width/10.75);
        high.getLayoutParams().width = (int) (width/4.24);

        junior.setTextColor(Color.parseColor("#ffffff"));
        middle.setTextColor(Color.parseColor("#0d4986"));
        high.setTextColor(Color.parseColor("#0d4986"));


        recyclerView.setLoadingMoreEnabled(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        pianoIndexAdapter = new PianoIndexAdapter(this, indexList);
        recyclerView.setAdapter(pianoIndexAdapter);
        postRanking.userid = BaseApplication.BasePreferences.readUID();
        postRanking.type = "1";
        postRanking.teacherid =BaseApplication.BasePreferences.readTeacherId();
//        postRanking.userid = "27";
        postRanking.page = "1";
        postRanking.execute();

        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                if(degree == 0){
                    postRanking.userid = BaseApplication.BasePreferences.readUID();
                    postRanking.teacherid = BaseApplication.BasePreferences.readTeacherId();
                    postRanking.type = "1";
//                    postQuestionRanking.teacherid = "3";
                    postRanking.page = "1";
                    postRanking.execute(context,false,0);
                }else if(degree == 1){
                    postRanking.userid = BaseApplication.BasePreferences.readUID();
                    postRanking.teacherid = BaseApplication.BasePreferences.readTeacherId();
                    postRanking.type = "2";
//                    postQuestionRanking.teacherid = "3";
                    postRanking.page = "1";
                    postRanking.execute(context,false,0);
                }else if(degree == 2){
                    postRanking.userid = BaseApplication.BasePreferences.readUID();
                    postRanking.teacherid = BaseApplication.BasePreferences.readTeacherId();
                    postRanking.type = "3";
//                    postQuestionRanking.teacherid = "3";
                    postRanking.page = "1";
                    postRanking.execute(context,false,0);
                }

            }

            @Override
            public void onLoadMore() {
                if(degree == 0){
//                    Log.e("=====page",currentInfo.per_page+"==page");
                    if(currentInfo.current_page*currentInfo.per_page<currentInfo.total){

                        postRanking.userid = BaseApplication.BasePreferences.readUID();
                        postRanking.type = "1";
                        postRanking.teacherid = BaseApplication.BasePreferences.readTeacherId();
//                        postQuestionRanking.teacherid = "3";
                        postRanking.page = currentInfo.current_page+1+"";
                        postRanking.execute(context,false,1);
                    }else {
                        Log.e("=====page","------22");
                        UtilToast.show("暂无更多数据");
                        recyclerView.refreshComplete();
                        recyclerView.loadMoreComplete();
                    }
                }else if(degree == 1){
//                    Log.e("=====page",currentInfo.per_page+"==page");
                    if(currentInfo.current_page*currentInfo.per_page<currentInfo.total){

                        postRanking.userid = BaseApplication.BasePreferences.readUID();
                        postRanking.type = "2";
                        postRanking.teacherid = BaseApplication.BasePreferences.readTeacherId();

//                        postQuestionRanking.teacherid = "3";
                        postRanking.page = currentInfo.current_page+1+"";
                        postRanking.execute(context,false,1);
                    }else {
                        Log.e("=====page","------22");
                        UtilToast.show("暂无更多数据");
                        recyclerView.refreshComplete();
                        recyclerView.loadMoreComplete();
                    }
                }else if(degree == 2){
//                    Log.e("=====page",currentInfo.per_page+"==page");
                    if(currentInfo.current_page*currentInfo.per_page<currentInfo.total){

                        postRanking.userid = BaseApplication.BasePreferences.readUID();
                        postRanking.type = "3";
                        postRanking.teacherid = BaseApplication.BasePreferences.readTeacherId();

//                        postQuestionRanking.teacherid = "3";
                        postRanking.page = currentInfo.current_page+1+"";
                        postRanking.execute(context,false,1);
                    }else {
                        Log.e("=====page","------22");
                        UtilToast.show("暂无更多数据");
                        recyclerView.refreshComplete();
                        recyclerView.loadMoreComplete();
                    }
                }

            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.junior:
                degree = 0;

                junior.setBackgroundResource(R.mipmap.ph_button_select);
                middle.setBackgroundResource(R.mipmap.ph_button_default);
                high.setBackgroundResource(R.mipmap.ph_button_default);

                junior.getLayoutParams().height =  width/9;
                junior.getLayoutParams().width = (int) (width/4.24);

                middle.getLayoutParams().height = (int) (width/10.75);
                middle.getLayoutParams().width = (int) (width/4.24);

                high.getLayoutParams().height = (int) (width/10.75);
                high.getLayoutParams().width = (int) (width/4.24);

                junior.setTextColor(Color.parseColor("#ffffff"));
                middle.setTextColor(Color.parseColor("#0d4986"));
                high.setTextColor(Color.parseColor("#0d4986"));
                postRanking.userid = BaseApplication.BasePreferences.readUID();
                postRanking.teacherid = BaseApplication.BasePreferences.readTeacherId();
//                postRanking.teacherid ="3";
//                postRanking.userid = "27";
                postRanking.type = "1";
//                    postQuestionRanking.teacherid = "3";
                postRanking.page = "1";
                postRanking.execute(context,false,0);
                break;

            case R.id.middle:

                degree = 1;
                junior.setBackgroundResource(R.mipmap.ph_button_default);
                middle.setBackgroundResource(R.mipmap.ph_button_select);
                high.setBackgroundResource(R.mipmap.ph_button_default);

                junior.getLayoutParams().height = (int) (width/10.75);
                junior.getLayoutParams().width = (int) (width/4.24);

                middle.getLayoutParams().height = (int) (width/9);
                middle.getLayoutParams().width = (int) (width/4.24);

                high.getLayoutParams().height = (int) (width/10.75);
                high.getLayoutParams().width = (int) (width/4.24);

                junior.setTextColor(Color.parseColor("#0d4986"));
                middle.setTextColor(Color.parseColor("#ffffff"));
                high.setTextColor(Color.parseColor("#0d4986"));
                postRanking.userid = BaseApplication.BasePreferences.readUID();
                postRanking.teacherid = BaseApplication.BasePreferences.readTeacherId();
//                postRanking.teacherid ="3";
//                postRanking.userid = "27";
                postRanking.type = "2";
//                    postQuestionRanking.teacherid = "3";
                postRanking.page = "1";
                postRanking.execute(context,false,0);
                break;

            case R.id.high:

                degree = 2;
                junior.setBackgroundResource(R.mipmap.ph_button_default);
                middle.setBackgroundResource(R.mipmap.ph_button_default);
                high.setBackgroundResource(R.mipmap.ph_button_select);

                junior.getLayoutParams().height = (int) (width/10.75);
                junior.getLayoutParams().width = (int) (width/4.24);

                middle.getLayoutParams().height = (int) (width/10.75);
                middle.getLayoutParams().width = (int) (width/4.24);

                high.getLayoutParams().height = (int) (width/9);
                high.getLayoutParams().width = (int) (width/4.24);

                junior.setTextColor(Color.parseColor("#0d4986"));
                middle.setTextColor(Color.parseColor("#0d4986"));
                high.setTextColor(Color.parseColor("#ffffff"));
                postRanking.userid = BaseApplication.BasePreferences.readUID();
                postRanking.teacherid = BaseApplication.BasePreferences.readTeacherId();
//                postRanking.teacherid ="3";
//                postRanking.userid = "27";
                postRanking.type = "3";
//                    postQuestionRanking.teacherid = "3";
                postRanking.page = "1";
                postRanking.execute(context,false,0);

                break;

            case R.id.last_week_question:

                startVerifyActivity(FriendOldListActivity.class);
                break;

        }
    }


}
